<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
	use HasDateTimeFormatter;


    protected $fillable = [
        'type',
        'user_id',
        'stats',
        'category_id',
        'title',
        'content',
        'media_type',
        'media_url',
        'cover',
        'status',
        'audit_status',
        'circle_id',
    ];

    protected $attributes = [
        'cover' => '',
        'circle_id' => 0,
        'stats' => 0,
        'status' => 1,
        'audit_status' => 1,
        'category_id' => 0
    ];

    protected $casts = [
        'media_url' => 'array',
        'task' => 'string'
    ];




    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }

    public function category()
    {
        return $this->belongsTo(ArticleCategory::class,'category_id');
    }

    public function like()
    {
        return $this->hasMany(UserLikeArticle::class,'article_id');
    }

    public function comment()
    {
        return $this->hasMany(ArticleComment::class,'article_id');
    }

    public function identity()
    {
        return $this->belongsTo(UserIdentity::class,'user_id','user_id');
    }

    public function collect()
    {
        return $this->hasMany(UserCollectArticle::class,'article_id');
    }


    public function getContentAttribute($value)
    {
        return str_replace("<img ", "<img style='width:100%;height:100%;vertical-align:middle;' ",$value);
    }


    public function follow()
    {
        return $this->belongsTo(UserFollow::class,'user_id','friend_id');
    }

    public function firm()
    {
        return $this->belongsTo(Firm::class,'user_id','user_id');
    }
    public function firms()
    {
        return $this->hasMany(Firm::class,'user_id','user_id');
    }

    public function score()
    {
        return $this->hasMany(ArticleScore::class,'article_id');
    }
}
